package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null){
			rebuildCache();
		}
		return cachedContacts;
	}
	
	
	
	private void rebuildCache() {
		manager.navigateTo().mainPage();
		cachedContacts = new SortedListOf<ContactData>();
		List<WebElement> lastNames = driver.findElements(By.xpath("//table/tbody/tr/td[2]"));
		List<WebElement> firstNames = driver.findElements(By.xpath("//table/tbody/tr/td[3]"));
		for (int i = 0; i< lastNames.size()-1; i++) {
			ContactData contact = new ContactData()
				.withFirstName(firstNames.get(i).getText())
				.withLastName(lastNames.get(i).getText());
			cachedContacts.add(contact);			
		}
	}



	public ContactHelper createContact(ContactData contact, boolean flag) {
		manager.navigateTo().addContactPage();
		fillContactForm(contact, flag);
    	submitContactCreation();
    	returnToContactPage();
    	rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact,
			boolean flag) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		fillContactForm(contact, flag);
		submitContactModification();
		returnToContactPage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		initContactModification(index);
		submitDeleteContact();
		returnToContactPage();
		rebuildCache();
		return this;
	}
	
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}

	public ContactHelper submitContactCreation() {
		click(By.name("submit"));
		cachedContacts = null;
		return this;
	}

	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"),contact.getFirstName());
		type(By.name("lastname"),contact.getLastName());
		type(By.name("address"),contact.getAddress());
		type(By.name("home"),contact.getHomeTelephone());
		type(By.name("mobile"),contact.getMobileTelephone());;
	 	type(By.name("work"),contact.getWorkTelephone());
	 	type(By.name("email"),contact.getEmail());
	 	type(By.name("email2"),contact.getSecondEmail());
	 	selectByText(By.name("bday"), contact.getDayBirthday());
	  	selectByText(By.name("bmonth"), contact.getMonthBirthday());
	    type(By.name("byear"),contact.getYearBirthday());
	    if (formType = CREATION) {
	    	//selectByText(By.name("new_group"), "group 1");
		} else {
			if (driver.findElements(By.name("new_group")).size()!=0){
				throw new Error("Group selector exists in contact modification form");
			}
		}
	    type(By.name("address2"),contact.getSecondAddress());
	    type(By.name("phone2"),contact.getSecondHomeTelephone());
	    return this;
	}

	public ContactHelper submitDeleteContact() {
		click(By.xpath("//form[2]/input[2]"));
		cachedContacts = null;
		return this;
		}

	public ContactHelper returnToContactPage() {
		click(By.linkText("home"));
		return this;
	}
	
	public ContactHelper initContactModification(int index) {
		selectContactByIndex(index);
		clickContactEditButtonByIndex(index);
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.name("update"));
		cachedContacts = null;
		return this;
	}

	protected void selectContactByIndex(int index) {
		index = index+1;
		click(By.xpath("//table[@id='maintable']/tbody/tr[" + index + "]/td/input"));
	}
	
	protected ContactHelper clickContactEditButtonByIndex(int index) {
		index = index+1;
		click(By.xpath("//tr["+ index + "]/td[7]/a"));
		return this;
	}

}
